﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PlayBox.DataClasses
{
    /// <summary>
    /// this class is used to store the that of all songs
    /// </summary>
    public class Music
    {
        public String ID { get; set; }
        public String Title { get; set; }
        public String Artist { get; set; }
        public Decimal Price { get; set; }
        public Stream SongStream { get; set; } 

        /// <summary>
        /// this method will play the song
        /// </summary>
        public void Play()
        {
            MusicPlayer.playSong(this);
        }
        /// <summary>
        /// this method will save the song to disk into the directory of the current user
        /// </summary>
        public void SaveToDisk()
        {
            string targetPath = User.UserData.GetDirectoryPath("Data\\" + ID);
            if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(targetPath)))
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(targetPath));
                    
            FileStream stream = new FileStream(targetPath, FileMode.Create);
            EncryptionLayer encr = new EncryptionLayer(stream,ID,false);

            SongStream.Seek(0, SeekOrigin.Begin);

            encr.writeStream(SongStream);
            encr.close();
        }
        /// <summary>
        /// initializes a stream from the disk
        /// </summary>
        /// <param name="_ID">the ID of the song</param>
        public void InitFromDisk(String _ID)
        {
            ID = _ID;
            string targetPath = User.UserData.GetDirectoryPath("Data\\" + ID);
            FileStream stream = new FileStream(targetPath, FileMode.Open);
            EncryptionLayer encr = new EncryptionLayer(stream,ID,true);
            SongStream = encr.getMemoryStream();
            encr.close();
        }

        /// <summary>
        /// this method is used for test purpose only and can be used to initialize a songstream from the disk
        /// </summary>
        /// <returns>a stream of D:\test.wav</returns>
        public static Stream getTestStream()
        {
            Stream stream = new FileStream("D:\\test.wav", FileMode.Open);
            return stream;
        }

    }
}
